package com.bcqn.controller;

import com.bcqn.common.CommonResponse;
import com.bcqn.enums.ConfigReturnType;
import com.bcqn.enums.ConfigType;
import com.bcqn.enums.YesyOrNo;
import com.bcqn.model.Config;
import com.bcqn.service.impl.ConfigService;
//import com.bcqn.service.ConfigService;
import com.bcqn.utils.FileUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/config")
public class ConfigController extends CommonController {


    @Resource
    private ConfigService configService;

    private void validateConfig(Config config) {
        if (ConfigType.of(config.getBannerType()) == null) {
            throw new RuntimeException("错误的请求");
        }
        if (ConfigReturnType.of(config.getReturnType()) == null) {
            throw new RuntimeException("错误的请求");
        }
        if (YesyOrNo.of(config.getOnline()) == null) {
            throw new RuntimeException("错误的请求");
        }
        if (StringUtils.isBlank(config.getTitle())) {
            throw new RuntimeException("名称不能为空");
        }
        if (config.getTitle().length() > 100) {
            throw new RuntimeException("名称超出最大长度");
        }
        if (config.getSeq() == null) {
            throw new RuntimeException("索引顺序不能为空");
        }
        if (config.getStartTime() != null && config.getEndTime() != null) {
            if (config.getStartTime().compareTo(config.getEndTime()) > 0) {
                throw new RuntimeException("结束时间不能早于开始时间");
            }
        }
    }

    @PostMapping("/add")
    public CommonResponse add(@RequestBody Config config) {
        validateConfig(config);
        config.setCreateTime(new Date());
        if (configService.add(config) == 1) {
            return CommonResponse.success();
        }
        return CommonResponse.fail("新增配置异常");
    }

    @PostMapping("/update")
    public CommonResponse update(@RequestBody Config config) {
        validateConfig(config);
        config.setUpdateTime(new Date());
        configService.update(config);
        return CommonResponse.success();
    }

    @GetMapping("/del")
    public CommonResponse del(@RequestParam("id") Long id) {
        Config config = configService.getById(id);
        if (configService.del(id) == 1) {
            if (StringUtils.isNotBlank(config.getDisplayImg())) {
                FileUtil.deleteByUrl(config.getDisplayImg());
            }
            return CommonResponse.success();
        }
        return CommonResponse.fail("删除配置异常");
    }

    @GetMapping("/getAll")
    public CommonResponse getAll(@RequestParam("type") int type, @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize, @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage) {
        return CommonResponse.success(configService.getAll(type, pageSize, currentPage));
    }

    @GetMapping("/queryOnline")
    public CommonResponse queryOnline(@RequestParam("type") int type) {
        Map<String, Object> map = new HashMap<>();
        map.put("configList", configService.queryOnline(type));
        return CommonResponse.success(map);
    }

    @PostMapping("/chargeOnlineStatus")
    public CommonResponse chargeOnlineStatus(@RequestParam("id") Long id, @RequestParam("status") Integer status) {
        Config config = configService.getById(id);
        config.setOnline(status);
        configService.update(config);
        return CommonResponse.success();
    }

    @GetMapping("/detail")
    public CommonResponse detail(@RequestParam("id") Long id) {
        return CommonResponse.success(configService.getById(id));
    }
}
